package com.ls.hash;

import java.util.Scanner;

public class HashTabDemo {
    public static void main(String[] args) {
        HashTab hashTab = new HashTab(7);
        String key;
        Scanner scanner = new Scanner(System.in);
        while (true) {
            System.out.print("add ");
            System.out.print("list ");
            System.out.print("find ");
            System.out.println("exit");

            key = scanner.next();
            switch (key) {
                case "add":
                    System.out.println("输入id");
                    int id = scanner.nextInt();
                    System.out.println("输入name");
                    String name = scanner.next();
                    hashTab.add(new Emp(id, name));
                    break;
                case "list":
                    hashTab.list();
                    break;
                case "find":
                    System.out.println("输入id");
                    int fid = scanner.nextInt();
                    hashTab.find(fid);
                    break;
                case "exit":
                    scanner.close();
                    System.exit(0);
                default:
                    break;
            }
        }

    }
}

class HashTab {
    private EmpLinkedList[] linkedListArray;

    private int size;

    public HashTab(int size) {
        this.size = size;
        linkedListArray = new EmpLinkedList[size];
        for (int i = 0; i < size; i++) {
            linkedListArray[i] = new EmpLinkedList();
        }
    }

    public void add(Emp emp) {
        //根据员工的id来决定放入第几个链表
        int hashFun = hashFun(emp.id);
        linkedListArray[hashFun].add(emp);
    }

    public void list() {
        for (int i = 0; i < size; i++) {
            linkedListArray[i].list(i + 1);
        }
    }
    public void find(int id) {
        int hashFun = hashFun(id);
        Emp emp = linkedListArray[hashFun].find(id);
        if (emp == null){
            System.out.println("没找到该雇员");
        }else {
            System.out.println(emp.toString());
        }

    }

    //编写散列函数,使用简单取模法
    public int hashFun(int id) {
        return id % size;
    }
}


class EmpLinkedList {
    //头节点指向第一个emp
    private Emp head;

    public void add(Emp emp) {
        if (head == null) {
            head = emp;
            return;
        }
        Emp curEmp = head;
        while (true) {
            if (curEmp.next == null) {
                break;
            }
            curEmp = curEmp.next;
        }
        curEmp.next = emp;

    }

    public Emp find(int id) {
        if (head == null) {
            return null;
        }
        Emp curEmp = head;
        while (true) {
            if (id == curEmp.id) {
                break;
            }
            if (curEmp.next == null) {
                curEmp = null;
            }
            curEmp = curEmp.next;
        }

        return curEmp;
    }


    public void list(int no) {
        if (head == null) {
            System.out.println("[" + no + "]链表为空");
            return;
        }
        Emp curEmp = head;
        System.out.print("[" + no + "]");
        while (true) {
            System.out.print(curEmp);
            if (curEmp.next == null) {
                break;
            }
            curEmp = curEmp.next;
        }
        System.out.println();


    }


}


class Emp {
    public int id;
    public String name;
    public Emp next;

    public Emp(int id, String name) {
        this.id = id;
        this.name = name;
    }

    public int getId() {
        return id;
    }

    public void setId(int id) {
        this.id = id;
    }

    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }

    public Emp getNext() {
        return next;
    }

    public void setNext(Emp next) {
        this.next = next;
    }

    @Override
    public String toString() {
        return "Emp{" +
                "id=" + id +
                ", name='" + name + '\'' +
                '}';
    }
}